package com.central.apps.handler;

import cn.hutool.core.map.MapUtil;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.util.SysUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author mengweitao
 * @Date 2022/11/22/022 20:08
 */
@Slf4j
@Component
public class MaterialUsenewHandler extends LvBaseHandler {

    @Autowired
    private HandlerService handlerService;


    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        /**
         * 当物料去向为被使用，该物料放到物料主数据里成品/半成品部件中
         */
        if("materialusenewApp".equals(app)){
            String materialgoing = MapUtils.getString(data, "materialgoing");
            if(StringUtils.isNotBlank(materialgoing) && "被使用".equals(materialgoing)){
                Map<String, Object> resultMap = new HashMap<>();
                String itemnum = MapUtils.getString(data, "itemnum");//贵重物料编码
                String finishedprocode = MapUtils.getString(data, "finishedprocode");//物料主数据编码
                String itemEnity_description = MapUtils.getString(data, "itemEnity_description");//贵重物料描述
                String outquantity = MapUtils.getString(data, "outquantity");//贵重物料数量
                String lotnum = MapUtils.getString(data, "lotnum");//贵重物料批次号
                String id = MapUtils.getString(data, "id");//贵重物料id
                String applyby = MapUtils.getString(data, "applyby");//接收人id
                String applyuserEntity_name = MapUtils.getString(data, "applyuserEntity_name");//接收人
                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_materialparts where materialtrackingid = " + id);
                if(CollectionUtils.isEmpty(bySql)){
                    resultMap.put("materialusenewnum",itemnum);
                    resultMap.put("itemnum",finishedprocode);
                    resultMap.put("itemdesc",itemEnity_description);
                    resultMap.put("innum",outquantity);
                    resultMap.put("batchnum",lotnum);
                    resultMap.put("materialtrackingid",id);
                    resultMap.put("applyby",applyby);
                    resultMap.put("applyuserEntity_name",applyuserEntity_name);
                    handlerService.insert("gq_materialparts",resultMap);
                }
                //修改贵重物料的状态为已完成
                String status="已完成";
                handlerService.findBySql("update gq_materialusenew set status = '" + status +"' where id = " + id);
            }
        }
        super.afterSave(app, objName, data, isUpdate);
    }

    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {
        /**
         * 只有贵重物料被消耗的才能发起流程
         */
        List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_materialusenew where id = " + id);
        if(CollectionUtils.isNotEmpty(bySql)){
            Map<String, Object> map = bySql.get(0);
            String materialgoing = MapUtils.getString(map, "materialgoing");
            if("被使用".equals(materialgoing)){
                return "物料被使用，不需要提交审批。";
            }
        }
        return super.beforeExcuteFlow(app, id, flowNode, op);
    }

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {

        /**
         * 被转移节点走下面逻辑
         */
        if(flowNode.getId().equals("transferee")){
            boolean flag = op.isFlag();//判断是否审核通过
            if(flag){
                /**
                 * 1.被转交人同意后，对这个被转交人继续生成一条消耗单
                 */
                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_materialusenew where id = " + id);
                if(CollectionUtils.isNotEmpty(bySql)){
                    Map<String, Object> materialuseMap = new HashMap<>();
                    Map<String, Object> materialuselistMap = new HashMap<>();
                    Map<String, Object> map = bySql.get(0);
                    String materialusenum = MapUtils.getString(map, "materialusenum");//编号
                    String description = MapUtils.getString(map, "description");//描述
//                    String location = MapUtils.getString(map, "location");//库房编号
//                    String locadesc = MapUtils.getString(map, "locadesc");//库房描述
                    String orgcodeEnityFullname = MapUtils.getString(map, "orgcodeEnity_fullname");//单位
                    String deptcodeEnityFullname = MapUtils.getString(map, "deptcodeEnity_fullname");//部门
//                    String projectEntityProname = MapUtils.getString(map, "projectEntity_proname");//项目名称
//                    String projectEntityPronum = MapUtils.getString(map, "projectEntity_pronum");//项目编号
//                    String proid = MapUtils.getString(map, "proid");//项目id
//                    String taskEntityName = MapUtils.getString(map, "taskEntity_name");//任务名称
//                    String taskid = MapUtils.getString(map, "taskid");//任务编号
//                    String materialgoing = MapUtils.getString(map, "materialgoing");//物料去向
                    String consumptiondescription = MapUtils.getString(map, "consumptiondescription");//消耗情况说明
                    String instructions = MapUtils.getString(map, "instructions");//使用说明
                    String applyuserEntityName = MapUtils.getString(map, "applyuserEntity_name");//被转交人
                    String applyby = MapUtils.getString(map, "applyby");//被转交人id person表id
                    String itemnum = MapUtils.getString(map, "itemnum");//物料编号
                    String itemEnityDescription = MapUtils.getString(map, "itemEnity_description");//物料名称
                    String company = MapUtils.getString(map, "company");//计量单位
                    String outquantity = MapUtils.getString(map, "outquantity");//出库数量
                    String unitcost = MapUtils.getString(map, "unitcost");//单价
                    String lotnum = MapUtils.getString(map, "lotnum");//批次
                    Date createtime = MapUtil.getDate(map, "createtime");//出库时间
//                    String locations = MapUtils.getString(map, "locations");//库房编号
//                    String pureason = MapUtils.getString(map, "Pureason");//用途及原因
                    String createuserid = MapUtils.getString(map, "createuser");//创建人

                    //根据personid查sysuser表id
                    String sysuserid = "";
                    List<Map<String, Object>> bySql1 = handlerService.findBySql("select id from sys_user where field10 ='" + applyby + "'");
                    if(CollectionUtils.isNotEmpty(bySql1)){
                        sysuserid = MapUtils.getString(bySql1.get(0), "id");
                    }
                    List<Map<String, Object>> materialusenumList = handlerService.findBySql("select materialusenum from gq_materialusenew ORDER by materialusenum desc");
                    int materialusenumnew=0;
                    if(CollectionUtils.isNotEmpty(materialusenumList)){
                        Map<String, Object> map1 = materialusenumList.get(0);
                        String materialusenum1 = MapUtils.getString(map1, "materialusenum");
                        materialusenumnew = Integer.parseInt(materialusenum1) + 1;
                        materialuseMap.put("materialusenum",materialusenumnew);//编号
                    }
                    materialuseMap.put("relationnum",materialusenum);//关联父编号
                    materialuseMap.put("relationnumnew",materialusenum);//关联父编号new
                    materialuseMap.put("description",description);//描述
                    materialuseMap.put("status","新建");//状态
//                    materialuseMap.put("location",location);//库房编号
//                    materialuseMap.put("locadesc",locadesc);//库房描述
//                    materialuseMap.put("projectEntity_proname",projectEntityProname);//项目名称
//                    materialuseMap.put("projectEntity_pronum",projectEntityPronum);//项目编号
//                    materialuseMap.put("proid",proid);//项目id
//                    materialuseMap.put("taskid",taskid);//任务编号
//                    materialuseMap.put("taskEntity_name",taskEntityName);//任务名称
                    materialuseMap.put("updateuser",SysUserUtil.getLoginAppUser().getId());//修改人
                    materialuseMap.put("createuser",sysuserid);//创建人
                    materialuseMap.put("createuserdesc",applyuserEntityName);//创建人描述
//                    materialuseMap.put("applyuserEntity_name",applyuserEntityName);//被转交人
                    materialuseMap.put("createtime",createtime);//创建时间
//                    materialuseMap.put("transferdate",new Date());//转移时间
                    materialuseMap.put("instructions",instructions);//使用说明
                    materialuseMap.put("consumptiondescription",consumptiondescription);//消耗情况说明
                    materialuseMap.put("materialgoing","未使用");//物料去向
                    materialuseMap.put("orgcodeEnity_fullname",orgcodeEnityFullname);//单位
                    materialuseMap.put("deptcodeEnity_fullname",deptcodeEnityFullname);//部门
                    materialuseMap.put("itemnum",itemnum);//物料编号
                    materialuseMap.put("itemEnity_description",itemEnityDescription);//物料描述
                    materialuseMap.put("company",company);//订购单位
                    materialuseMap.put("outquantity",outquantity);//出库数量
                    materialuseMap.put("unitcost",unitcost);//单价
                    materialuseMap.put("lotnum",lotnum);//批次
//                    materialuseMap.put("locations",locations);//库房编号
//                    materialuseMap.put("Pureason",pureason);//库房编号
                    handlerService.insert("gq_materialusenew",materialuseMap);//往物料消耗主表
                    handlerService.updateBySql("update gq_materialusenew set transferdate = NOW() where id = " + id);

//                    //子表物料列表
//                    List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_materialuselistnew where maternum = " + materialusenum);
//                    if(CollectionUtils.isNotEmpty(bySql1)){
//                        Map<String, Object> map1 = bySql1.get(0);
//                        String itemnum = MapUtils.getString(map1, "itemnum");//物料编号
//                        String itemEnityDescription = MapUtils.getString(map1, "itemEnity_description");//物料名称
//                        String company = MapUtils.getString(map1, "company");//计量单位
//                        String outquantity = MapUtils.getString(map1, "outquantity");//出库数量
//                        String unitcost = MapUtils.getString(map1, "unitcost");//单价
//                        String lotnum = MapUtils.getString(map1, "lotnum");//批次
//                        String locations = MapUtils.getString(map1, "locations");//库房编号
//                        String pureason = MapUtils.getString(map1, "Pureason");//用途及原因
//                        String createuserid = MapUtils.getString(map1, "createuser");//创建人
//                        materialuselistMap.put("maternum",materialusenumnew);//关联主表编号
//                        materialuselistMap.put("itemnum",itemnum);//物料编号
//                        materialuselistMap.put("itemEnity_description",itemEnityDescription);//物料描述
//                        materialuselistMap.put("company",company);//订购单位
//                        materialuselistMap.put("outquantity",outquantity);//出库数量
//                        materialuselistMap.put("unitcost",unitcost);//单价
//                        materialuselistMap.put("lotnum",lotnum);//批次
//                        materialuselistMap.put("locations",locations);//库房编号
//                        materialuselistMap.put("Pureason",pureason);//库房编号
//                        //创建人创建时间
//                        materialuselistMap.put("updateuser",createuserid);//修改人
//                        materialuselistMap.put("createuser",createuserid);//创建人
//                        materialuselistMap.put("updatetime",new Date());//修改时间
//                        materialuselistMap.put("createtime",new Date());//创建时间
//
//                        handlerService.insert("gq_materialusenew",materialuseMap);//往物料消耗主表
//                        handlerService.insert("gq_materialuselistnew",materialuselistMap);//往物料消耗子表
//                    }
                }
            }
        }
        super.afterExcuteFlow(app, id, flowNode, op, event);
    }
}
